<script setup lang="ts">
import { ref, watch, onMounted, onBeforeUnmount  } from 'vue'
 
const isNews = ref(true);

const scrollingContainer = ref(null);

// 告警信息
const scrollingItems = ref([
  {
    title: '网络告警001',
    detail: '来自设备001在2024-04-15 03:18:17',
  },
  {
    title: '网络告警002',
    detail: '来自设备001在2024-04-15 03:18:17',
  },
  {
    title: '004',
    detail: '来自设备007在2024-04-15 03:18:17',
  },
  {
    title: '005',
    detail: '2024-04-15 03:18:17',
  }
])

let timeoutId = null;
let isScrollingPaused = ref(false);

// 监听 scrollingItems 的变化
watch(scrollingItems, () => {
  // 检查是否需要重新开始滚动
  if (scrollingContainer.value.scrollTop >= scrollingContainer.value.scrollHeight - scrollingContainer.value.clientHeight) {
    startScrolling();
  }
});

const exchange = () => {
  isNews.value = !isNews.value;
  scrollingContainer.value.scrollTop = 0;
}

// 在组件挂载后开始滚动
onMounted(() => {
  startScrolling();
});

// 开始滚动
const startScrolling = () => {
  if (!isScrollingPaused.value) {
    clearTimeout(timeoutId);
    timeoutId = setTimeout(() => {
      scrollContent()
    }, 3000);
  }
};

// 滚动内容
const scrollContent = () => {
  scrollingContainer.value.scrollTop += 40;
  if (scrollingContainer.value.scrollTop >= scrollingContainer.value.scrollHeight - scrollingContainer.value.clientHeight) {
    scrollingContainer.value.scrollTop = 0;
  }
  startScrolling(); // 继续滚动
};

const pauseScroll = () => {
  clearTimeout(timeoutId);
}

onBeforeUnmount(() => {
  clearTimeout(timeoutId);
})

const resumeScroll = () => {
  startScrolling()
}


</script>

<template>
  <div class="news-wrapper">
    <div v-show="!isNews" @click="exchange" class="news-icon"></div>
    <div v-show="isNews" class="news-con">
      <div class="delete-icon" @click="exchange"></div>
      <div class="line"></div>
      <div class="con-info"
        ref="scrollingContainer" 
        @mouseenter="pauseScroll"
        @mouseleave="resumeScroll"
      >
        <div
          class="scrolling-item"
          v-for="(item, index) in scrollingItems"
          :key="index"
        >
          <div class="title">{{ item.title }}</div>
          <div class="detail">{{ item.detail }}</div>
        </div>
      </div>
    </div>
    <div class="news-arrow"></div>
  </div>
</template>

<style scoped lang="stylus">
.news-wrapper {
  height: 40px;
  width: 344px;
  display: flex;
  justify-content: flex-end;
  .news-icon {
    width: 25px;
    height: 25px;
    margin-top: 7px;
    background: url('@/assets/home/news_icon.png') no-repeat;
    cursor pointer;
  }
  .news-con {
    width: 310px;
    height: 40px;
    display: flex;
    justify-content: flex-start;
    .delete-icon {
      width: 10px;
      height: 12px;
      margin: 3px 8px 0 0;
      background: url('@/assets/home/news_delete.png') no-repeat;
      cursor: pointer;
    }
    .line {
      width: 2px;
      height: 34px;
      background: rgba(255, 53, 53, 1);
      margin: 3px 11px 0 0;
    }
    .con-info {
      flex: 1;
      height: 40px;
      display: flex;
      justify-content: space-between;
      flex-direction: column;
      overflow-y: scroll;
      overflow: hidden;
      .scrolling-item  {
        height: 40px;
        line-height: 40px;
      }
      .title {
        line-height: 22px;
        font-size: 14px;
        font-weight: 500;
        color: #FFFFFF;
      }
      .detail {
        line-height: 18px;
        font-size: 10px;
        font-weight: 500;
        color: #8F8E8E;
      }
    }
  }
  .news-arrow {
    width: 12px;
    height: 8px;
    margin: 16px 12px 0 6px;
    background: url('@/assets/home/news_arrow.png') no-repeat;
    cursor pointer
  }
}
</style>
<style lang="stylus">
@keyframes scrollAnimation {
  0% {
    transform: translateY(0);
  }
  100% {
    transform: translateY(-100%);
  }
}
</style>